package ch.fusun.baron.basic.client.ui;

import ch.fusun.baron.basic.command.MoveUnitCommand;
import ch.fusun.baron.client.rmi.ClientService;
import ch.fusun.baron.core.injection.Inject;
import ch.fusun.baron.core.service.UserService;
import ch.fusun.baron.map.Tile;
import ch.fusun.baron.map.api.GameMapService;
import ch.fusun.baron.player.Dynasty;
import ch.fusun.baron.player.api.PlayerService;
import ch.fusun.baron.unit.Unit;
import ch.fusun.baron.unit.service.UnitService;

/**
 * Move a unit (abstract impl)
 */
public abstract class MoveUnitTileAction extends AbstractTileAction {

	@Inject
	private UnitService unitService;
	@Inject
	private UserService userService;
	@Inject
	private PlayerService playerService;
	@Inject
	private GameMapService mapService;
	@Inject
	private ClientService clientService;

	@Override
	public Class<?> getReactionClass() {
		return Unit.class;
	}

	@Override
	public void execute(Object object) {
		if (object instanceof Unit) {
			Unit unit = (Unit) object;
			Tile location = unitService.getLocation(unit);
			Dynasty dynasty = playerService.getDynasty(userService.getUser());
			int width = mapService.getWidth();
			int height = mapService.getHeight();
			clientService.execute(new MoveUnitCommand(dynasty, unit, new Tile(
					(location.getX() + getHorizontalDiff() + width) % width,
					(location.getY() + getVerticalDiff() + height) % height)));
		}
	}

	/**
	 * @return the vertical translation
	 */
	protected abstract int getVerticalDiff();

	/**
	 * @return the horizontal translation
	 */
	protected abstract int getHorizontalDiff();

}
